package com.taxi.driver.module.account.identity;

import android.text.TextUtils;

import com.qianxx.utils.RxUtil;
import com.qianxx.utils.encode.RandomUtil;
import com.qianxx.utils.text.RegUtils;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.user.UserRepository;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class IdentifyPresenter extends BasePresenter implements IdentifyContract.Presenter {

    UserRepository mUserRepository;
    IdentifyContract.View mView;

    @Inject
    public IdentifyPresenter(UserRepository userRepository, IdentifyContract.View view) {
        mUserRepository = userRepository;
        mView = view;
    }

    @Override
    public void reqVerify(final String phone, final String idCard) {
        if (TextUtils.isEmpty(phone)) {
            mView.toast(R.string.login_empty_phone);
            return;
        }
        if (!RegUtils.isMobileNumber(phone)) {
            mView.toast(R.string.login_error_phone);
            return;
        }
        if (TextUtils.isEmpty(idCard)) {
            mView.toast(R.string.empty_id_card);
            return;
        }
        if (idCard.length() != 7 && idCard.length() != 18) {
            mView.toast(R.string.error_id_card);
            return;
        }
        mSubscriptions.add(mUserRepository.validate(RandomUtil.RandomEncrypt(phone), RandomUtil.RandomEncrypt(idCard),
                mUserRepository.getDriverType())
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doAfterTerminate(() -> mView.hideLoadingView())
                .subscribe(s -> {
                    mView.verifySuccess(phone, idCard);
                }, ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                }));

    }
}
